home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Archive / Games / Soundboard / CFilterControl.cp < prev    next >
Encoding:
Text File  |  2000-09-28  |  7.7 KB  |  312 lines  |  [TEXT/MMCC]

  1. /*
  2. ** File:        CFilterControl.cp
  3. ** Written by:  Tim Nufire
  4. **
  5. ** Copyright © 1990-1995 Apple Computer, Inc.
  6. ** All rights reserved. */
  7.  
  8. /*****************************************************************************/
  9. /* This is a custom Filter slider.                                             */
  10. /*****************************************************************************/
  11.  
  12.  
  13. #include "CFilterControl.h"
  14. #include "CSoundboardApp.h"
  15.  
  16. #include "FilterComponent.h"
  17.  
  18. #include <UAppleEventsMgr.h>
  19. #include <UExtractFromAEDesc.h>
  20. #include <LModelProperty.h>
  21.  
  22. #ifndef __AEPACKOBJECT__
  23. #include <AEPackObject.h>
  24. #endif
  25.  
  26. #ifndef __AEOBJECTS__
  27. #include <AEObjects.h>
  28. #endif
  29.  
  30. #ifndef __AEREGISTRY__
  31. #include <AERegistry.h>
  32. #endif
  33.  
  34.  
  35. // ---------------------------------------------------------------------------
  36. //        • CreateFilterControlStream [static]
  37. // ---------------------------------------------------------------------------
  38. //    Create a new FilterControl from the data in a Stream
  39. //
  40. //    Current port must be the Window into which to install the control
  41.  
  42. CFilterControl*
  43. CFilterControl::CreateFilterControlStream(
  44.     LStream    *inStream)
  45. {
  46.     return (new CFilterControl(inStream));
  47. }
  48.  
  49. // ---------------------------------------------------------------------------
  50. //        • FilterControl(LStream*)
  51. // ---------------------------------------------------------------------------
  52. //    Construct SliderControl from a data stream
  53.  
  54. CFilterControl::CFilterControl(
  55.     LStream    *inStream)
  56.         : CSliderControl(inStream)
  57. {
  58.     mLabel = 0;
  59.     mLabelChanges = 0;
  60.  
  61.     SetModelKind(cSlider);
  62.     SetUseSubModelList(false);
  63. }
  64.  
  65. // ---------------------------------------------------------------------------
  66. //        • ~FilterControl
  67. // ---------------------------------------------------------------------------
  68. //    Destructor
  69.  
  70. CFilterControl::~CFilterControl()
  71. {
  72. }
  73.  
  74.  
  75. // ===========================================================================
  76. // • AppleEvent Object Model Support         AppleEvent Object Model Support •
  77. // ===========================================================================
  78.  
  79.  
  80. void
  81. CFilterControl::GetAEProperty(
  82.     DescType        inProperty,
  83.     const AEDesc    &inRequestedType,
  84.     AEDesc            &outPropertyDesc) const
  85. {
  86.     OSErr    err;
  87.  
  88.     switch (inProperty) {
  89.     
  90.         case pValue:
  91.             Int32    theValue = GetValue();
  92.             err = AECreateDesc(typeLongInteger, (Ptr) &theValue,
  93.                                 sizeof(Int32), &outPropertyDesc);
  94.             break;
  95.         
  96.         case pMaxValue:
  97.             Int32    theMaxValue = GetMaxValue();
  98.             err = AECreateDesc(typeLongInteger, (Ptr) &theMaxValue,
  99.                                 sizeof(Int32), &outPropertyDesc);
  100.             break;
  101.         
  102.         case pName:                    // Slider Title
  103.             Str255    theName;
  104.             GetDescriptor(theName);
  105.             err = ::AECreateDesc(typeChar, (Ptr) theName + 1,
  106.                                 StrLength(theName), &outPropertyDesc);
  107.             break;
  108.     
  109.         case pLabel:                // Slider Label
  110.             if (mLabel) {
  111.                 Str255    theLabel;
  112.                 mLabel->GetDescriptor(theLabel);
  113.                 err = ::AECreateDesc(typeChar, (Ptr) theLabel + 1,
  114.                                     StrLength(theLabel), &outPropertyDesc);
  115.             }
  116.             else LModelObject::GetAEProperty(inProperty, inRequestedType,
  117.                                             outPropertyDesc);
  118.             break;
  119.     
  120.         case pBounds:                // Frame in local coords
  121.             Rect    theBounds;
  122.             CalcLocalFrameRect(theBounds);
  123.             err = ::AECreateDesc(typeQDRectangle, (Ptr) &theBounds,
  124.                                 sizeof(Rect), &outPropertyDesc);
  125.             break;
  126.             
  127.         case pIndex:                // Front-to-back position index
  128.             Int32    thePosition = mSuperModel->GetPositionOfSubModel(cSlider, this);
  129.             err = ::AECreateDesc(typeLongInteger, (Ptr) &thePosition,
  130.                                 sizeof(Int32), &outPropertyDesc);
  131.             break;
  132.             
  133.         case pVisible:
  134.             Boolean    isVis = IsVisible();
  135.             err = AECreateDesc(typeBoolean, (Ptr) &isVis,
  136.                                 sizeof(Boolean), &outPropertyDesc);
  137.             break;
  138.             
  139.         default:
  140.             LModelObject::GetAEProperty(inProperty, inRequestedType,
  141.                                             outPropertyDesc);
  142.             break;
  143.     }
  144. }
  145.  
  146.  
  147. void
  148. CFilterControl::SetAEProperty(
  149.     DescType        inProperty,
  150.     const AEDesc    &inValue,
  151.     AEDesc&            outAEReply)
  152. {
  153.     switch (inProperty) {
  154.         case pValue:
  155.             Int32    theValue;
  156.             UExtractFromAEDesc::TheInt32(inValue, theValue);
  157.             SliderAction(theValue);
  158.             SetValue(theValue);
  159.             break;
  160.         
  161.         case pMaxValue:
  162.             Int32    theMaxValue;
  163.             UExtractFromAEDesc::TheInt32(inValue, theMaxValue);
  164.             SetMaxValue(theMaxValue);
  165.             break;
  166.         
  167.         case pName:
  168.             Str255    theName;
  169.             UExtractFromAEDesc::ThePString(inValue, theName);
  170.             SetDescriptor(theName);
  171.             break;
  172.     
  173.         case pLabel:
  174.             if (mLabel) {
  175.                 Str255    theLabel;
  176.                 UExtractFromAEDesc::ThePString(inValue, theLabel);
  177.                 mLabel->SetDescriptor(theLabel);
  178.                 mLabel->Refresh();
  179.             }
  180.             else LModelObject::SetAEProperty(inProperty, inValue, outAEReply);
  181.             break;
  182.     
  183.         case pBounds:
  184.             Rect    theNewBounds, theOldBounds;
  185.             UExtractFromAEDesc::TheRect(inValue, theNewBounds);
  186.             CalcLocalFrameRect(theOldBounds);
  187.             MoveBy(theNewBounds.left-theOldBounds.left, theNewBounds.top-theOldBounds.top, true);
  188.             ResizeFrameBy((theNewBounds.right-theNewBounds.left) - (theOldBounds.right - theOldBounds.left),
  189.                           (theNewBounds.bottom-theNewBounds.top) - (theOldBounds.bottom - theOldBounds.top),
  190.                           true);
  191.             break;
  192.             
  193.         case pVisible:
  194.             Boolean    makeVisible;
  195.             UExtractFromAEDesc::TheBoolean(inValue, makeVisible);
  196.             if (makeVisible) {
  197.                 Show();
  198.                 if (mLabel) mLabel->Show();
  199.             } else {
  200.                 Hide();
  201.                 if (mLabel) mLabel->Hide();
  202.             }
  203.             break;
  204.             
  205.         default:
  206.             LModelObject::SetAEProperty(inProperty, inValue, outAEReply);
  207.             break;
  208.     }
  209. }
  210.  
  211.  
  212. void
  213. CFilterControl::HandleAppleEvent(
  214.     const AppleEvent    &inAppleEvent,
  215.     AppleEvent            &outAEReply,
  216.     AEDesc                &outResult,
  217.     long                inAENumber)
  218. {
  219.     Int32            value;
  220.     StAEDescriptor    valDesc;
  221.  
  222.     switch (inAENumber) {
  223.     
  224.         case ae_SetValue:
  225. /*            valDesc.GetParamDesc(inAppleEvent, keyDirectObject, typeLongInteger);
  226.             UExtractFromAEDesc::TheInt32(valDesc.mDesc, value);
  227.  
  228.             SetValue(value);
  229. */
  230. SysBeep(10);
  231.             break;
  232.             
  233.         case ae_SetMax:
  234.             valDesc.GetParamDesc(inAppleEvent, keyDirectObject, typeLongInteger);
  235.             UExtractFromAEDesc::TheInt32(valDesc.mDesc, value);
  236.  
  237.             SetMaxValue(value);
  238.             break;
  239.             
  240.         default:
  241.             LModelObject::HandleAppleEvent(inAppleEvent, outAEReply,
  242.                                                 outResult, inAENumber);
  243.             break;
  244.     }
  245. }
  246.  
  247.  
  248. // ===========================================================================
  249. // • Sending Apple Events                                Sending Apple Events •
  250. // ===========================================================================
  251.  
  252. // ---------------------------------------------------------------------------
  253. //        • SendAESetValue
  254. // ---------------------------------------------------------------------------
  255. //    AppleEvent for setting the value of a slider
  256.  
  257. void
  258. CFilterControl::SendAESetValue(
  259.     Int32    value,
  260.     Boolean    inExecute)
  261. {
  262.     LModelProperty    valueProperty(pValue, this, false);
  263.     valueProperty.SendSetDataAE(typeLongInteger, (Ptr) &value,
  264.                                         sizeof(Int32), inExecute);
  265. }
  266.  
  267.  
  268. // ---------------------------------------------------------------------------
  269. //        • SendAESetMax
  270. // ---------------------------------------------------------------------------
  271. //    AppleEvent for setting the Max of a slider
  272.  
  273. void
  274. CFilterControl::SendAESetMax(
  275.     Int32    max)
  276. {
  277.     LModelProperty    maxValueProperty(pMaxValue, this, false);
  278.     maxValueProperty.SendSetDataAE(typeLongInteger, (Ptr) &max,
  279.                                         sizeof(Int32), false);
  280. }
  281.  
  282.  
  283. void    
  284. CFilterControl::TrackSlider(Point origMouseLoc)
  285. {
  286.     short gainIndex = GetPaneID()-FirstSliderID;
  287.     
  288.     CSliderControl::TrackSlider(origMouseLoc);
  289.     
  290.     SliderAction((*mMacControlH)->contrlValue);
  291.     SendAESetValue((*mMacControlH)->contrlValue);
  292. }
  293.  
  294. void    
  295. CFilterControl::SliderAction(short newPos)
  296. {
  297.     short gainIndex = GetPaneID()-FirstSliderID;
  298.     
  299.     if (CSoundboardApp::mFilterInstance)
  300.     {
  301.         double curGain,  newGain = ((double)kMaxGain - newPos)/kMaxGain;
  302.         
  303.         DoGetGain (CSoundboardApp::mFilterInstance, gainIndex, &curGain);
  304.         
  305.         if (curGain != newGain)
  306.             DoSetGain (CSoundboardApp::mFilterInstance, gainIndex, &newGain);
  307.     }
  308.     
  309.     CSoundboardApp::mGainValue[gainIndex] = newPos;
  310. }
  311.  
  312.